package com.tanhua.server.controller;


import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.api.R;
import com.tanhua.server.service.QuanZiService;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.QuanZiVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.Map;

@RestController
@RequestMapping("/movements")
public class QuanZiController {
    @Autowired
    private QuanZiService quanZiService;

    /**
     * 查询好友动态
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public PageResult queryPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return quanZiService.queryPublishList(page, pageSize);
    }

    @PostMapping
    public ResponseEntity<Void> savePublish(String textContent, MultipartFile[] imageContent,
                                            String location, String longitude, String latitude) {

        try {
            String publishId = quanZiService.savePublish(textContent, imageContent, location, longitude, latitude);
            if (null != publishId) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("/recommend")
    public PageResult queryRecommendPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return this.quanZiService.queryRecommendPublishList(page, pageSize);
    }

    @GetMapping("/{id}/like")
    public ResponseEntity<Integer> likeComment(@PathVariable("id") String publishId) {
        try {
            Integer publishLikeCount = quanZiService.likeComment(publishId);
            if (null != publishLikeCount) {
                return ResponseEntity.ok(publishLikeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping(("/{id}/dislike"))
    public ResponseEntity<Integer> disLikeComment(@PathVariable("id") String publishId) {
        try {
            Integer publishLikeCount = this.quanZiService.disLikeComment(publishId);
            if (publishLikeCount != null) {
                return ResponseEntity.ok(publishLikeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("/{id}/love")
    public ResponseEntity<Integer> loveComment(@PathVariable("id") String publishId) {
        try {
            Integer commentLoveCount = this.quanZiService.loveComment(publishId);
            if (null != commentLoveCount) {
                return ResponseEntity.ok(commentLoveCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("/{id}/unlove")
    public ResponseEntity<Integer> unLoveComment(@PathVariable("id") String publishId) {
        try {
            Integer commentLoveCount = this.quanZiService.unLoveComment(publishId);
            if (null != commentLoveCount) {
                return ResponseEntity.ok(commentLoveCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * TODO：谁看过我;为了不让软件报错,先写一个空的方法;后续进行实现
     * 否则前端传一个visitors路径,会误以为是queryById方法的{id}参数;然后一直报错
     *
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity<Object> queryVisitors() {
        return ResponseEntity.ok(Collections.EMPTY_LIST);
    }

    /**
     * TODO：谁看过我;为了不让软件报错,先写一个空的方法;后续进行实现
     * 否则前端传一个visitors路径,会误以为是queryById方法的{id}参数;然后一直报错
     *
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity<Object> all() {
        return ResponseEntity.ok(Collections.EMPTY_LIST);
    }

    @GetMapping("/{id}")
    public ResponseEntity<QuanZiVo> queryById(@PathVariable("id") String publishId) {
        try {
            QuanZiVo movements = this.quanZiService.queryById(publishId);
            if (null != movements) {
                return ResponseEntity.ok(movements);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }



}
